class Watcher {
  constructor(vm, name, cb) {
    Dep.target = this // Dep.target 是一个全局变量
    this.vm = vm
    this.name = name
    this.cb = cb
    this.update()
    Dep.target = null
  }

  update() {
    let newValue = this.getVal(this.vm, this.name)
    this.cb(newValue)
    // this.node.nodeValue = this.getVal(this.vm, this.name) // html 值变化更改 data 对应的值
    // this.node.value = this.getVal(this.vm, this.name) // data 值赋值到html 中的数据绑定的值
  }

  getVal(vm, key) {
    let keys = key.split('.')
    return keys.reduce((prev, next) => { // vm.$data.a.b
      return prev[next]
    }, vm.$data)
  }

}